/**
 * @filename Logging.java
 * @package com.tianjian.dass.system[dass_pro]
 * @description 日志记录器
 * @author 胡正衛 
 * @date 2012-7-23 下午2:03:10
 * @version v0.1
 */
package com.tianjian.common.logging;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import com.tianjian.common.BaseAction;
import com.tianjian.common.Criteria;
import com.tianjian.oa.system.pojos.UserInfo;
import com.tianjian.util.GeneralUitl;

/**
 * 日志记录器
 * @author 胡正衛
 */
public class Logging {

	private LoggingMapper loggingMapper;

	/**
	 * 系统运行日志
	 * @param action
	 */
	public void log(HttpServletRequest request, Object object, UserInfo user, long spend, Throwable ex) {
		try {
			String browser = request.getHeader("User-Agent");
			String ip = GeneralUitl.GetRealIP(request);
			String clazz = "[" + ((BaseAction) object).getActionName() + "]" + object.getClass().getName();
			String path = request.getServletPath();
			String param = getParamList(request);
			String throwable = getThrowable(ex);
			Criteria criteria = new Criteria();
			criteria.put("sessionid", user.getSessionId());
			criteria.put("user", user.getId());
			criteria.put("path", path);
			criteria.put("class", clazz);
			criteria.put("param", param);
			criteria.put("throwable", throwable);
			criteria.put("browser", browser);
			criteria.put("ip", ip);
			criteria.put("spend", spend);
			loggingMapper.log(criteria);
		} catch (Exception e) {
			Exception exception = new Exception("系统日志写入出错.", e);
			exception.printStackTrace();
		}
	}

	/**
	 * 用户登陆日志
	 */
	public void login(UserInfo user) {
		try {
			Criteria criteria = new Criteria();
			criteria.put("sessionid", user.getSessionId());
			criteria.put("user", user.getId());
			criteria.put("ip", user.getLoginip());
			loggingMapper.login(criteria);
		} catch (Exception e) {
			Exception exception = new Exception("登陆日志写入出错.", e);
			exception.printStackTrace();
		}
	}

	/**
	 * 获取request中的参数
	 * @param request
	 * @return
	 */
	private String getParamList(HttpServletRequest request) {
		String result = "";
		Enumeration<?> url = request.getParameterNames();
		if (url != null) {
			while (url.hasMoreElements()) {
				String _pare = url.nextElement().toString();
				String _val = "";
				_val = request.getParameter(_pare);
				result += ",[" + _pare + ":" + _val + "]";
			}
		}
		return result.length() == 0 ? null : result.substring(1);
	}

	/**
	 * 获取错误信息
	 * @param e
	 * @return
	 */
	private String getThrowable(Throwable e) {
		if (e == null) return null;
		String s = e.getClass().toString();
		String message = e.getLocalizedMessage();
		String result = ((message != null) ? (s + ": " + message) : s) + "\r\n";
		StackTraceElement[] stes = e.getStackTrace();
		for (int i = 0; i < stes.length; i++) {
			result += "\t" + stes[i].toString() + "\r\n";
		}
		return result;
	}

	public void setLoggingMapper(LoggingMapper loggingMapper) {
		this.loggingMapper = loggingMapper;
	}

}
